package com.lpf.back.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lpf.back.common.CommonRes;
import com.lpf.back.domain.Activity;
import com.lpf.back.domain.Comment;
import com.lpf.back.domain.Dynamic;
import com.lpf.back.domain.User;
import com.lpf.back.domain.vo.UserCommentVO;
import com.lpf.back.mapper.CommentMapper;
import com.lpf.back.mapper.DynamicMapper;
import com.lpf.back.mapper.UserMapper;
import com.lpf.back.service.interfaces.UserService;
//import net.sf.jsqlparser.statement.comment.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
//import java.sql.Date;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service("userService1")
@Transactional
public class UserServiceImp implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DynamicMapper dynamicMapper;

    @Autowired
    private CommentMapper commentMapper;

    private User getUser(String username){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return userMapper.selectOne(wrapper);
    }


    @Override
    public CommonRes login(String username, String password) {
        User user = getUser(username);
        if(user == null || !user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))){
            return new CommonRes(null, 0, "账号或密码错误！");
        } else if (user.getStatus() == 1 && user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
            return new CommonRes(user, 1, "登录成功！");
        } else {
            return new CommonRes(null, 2, "当前用户不可用，请联系管理员！");
        }

    }

    @Override
    public int register(User user) {
        user.setBeginData(new Date(System.currentTimeMillis()));
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setStatus(1);
        return userMapper.insert(user);
    }

    @Override
    public User getInfo(String username) {
        return getUser(username);
    }

    @Override
    public void userLikeActivity(User user, Activity activity) {
        activity.setLikes(activity.getLikes() + 1);
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectList(null);
    }

    @Override
    public int setNickName(String nickname, Integer id) {
        User user = userMapper.selectById(id);
        user.setNickname(nickname);
        return userMapper.updateById(user);
    }

    @Override
    public int changePassword(String password, Integer id) {
        User user = userMapper.selectById(id);
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        return userMapper.updateById(user);
    }

    @Override
    public CommonRes setAvatar(HttpServletRequest request) {
        MultipartHttpServletRequest params=((MultipartHttpServletRequest) request);
        MultipartFile avatarImage = ((MultipartHttpServletRequest) request).getFile("avatarImage");
        String id = params.getParameter("id");
        System.out.println(id);
        if(!avatarImage.isEmpty()){

        }
        CommonRes res = new CommonRes();
        return res;
    }

    @Override
    public Dynamic likes(Integer dynamicid, Integer userid) {
        Dynamic dynamic = dynamicMapper.selectById(dynamicid);
        String likeid = dynamic.getLikeid();
        List<Integer> userIdList = new ArrayList<>();
        if(likeid != null){
            userIdList = JSON.parseArray(likeid, Integer.class);
            if(!userIdList.contains(userid)){
                dynamic.setLikes(dynamic.getLikes() + 1);
                userIdList.add(userid);
                String newUserIdList = JSON.toJSONString(userIdList);
                dynamic.setLikeid(newUserIdList);
                dynamicMapper.updateById(dynamic);
                return dynamic;
            } else {
                return null;
            }
        }
        dynamic.setLikes(1);
        userIdList.add(userid);
        dynamic.setLikeid(JSON.toJSONString(userIdList));
        dynamicMapper.updateById(dynamic);
        return dynamic;
    }

    @Override
    public List<UserCommentVO> comment(Integer dynamicid) {
        List<UserCommentVO> userCommentVOS = commentMapper.selectUserCommentVOList(dynamicid);
        for (UserCommentVO userCommentVO : userCommentVOS) {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            userCommentVO.setCommentTime(format.format(userCommentVO.getTime()));
        }
        return userCommentVOS;
    }

    @Override
    public Comment commitComment(Comment comment) {
        comment.setTime(new Date(System.currentTimeMillis()));
        comment.setId(null);
        commentMapper.insert(comment);
        return comment;
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public int updateStatus(Integer id) {
        User user = userMapper.selectById(id);
        if(user.getStatus() == 1){
            user.setStatus(0);
        } else {
            user.setStatus(1);
        }
        return userMapper.updateById(user);
    }

}
